/**
 * WechatDaoImpl.java created by zhangzhidong 
 * at 下午7:26:08 2016年4月18日
 */
package dao.wechatlab.impl;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import dao.BaseDao;
import dao.wechatlab.WechatDao;
import pojo.weichatlab.Label;
import pojo.weichatlab.Wechat;
import pojo.weichatlab.WechatClassMap;

/**
 * @author zhangzhidong
 */
@Repository
@SuppressWarnings("unchecked")
public class WechatDaoImpl extends BaseDao implements WechatDao {

	/**
	 * @Param
	 * @Author zhangzhidong
	 */

	public List<Wechat> selectWechatById(String id) {
		DetachedCriteria dc = DetachedCriteria.forClass(Wechat.class);
		dc.add(Restrictions.eq("id", Integer.parseInt(id)));
		return (List<Wechat>) this.getHibernateTemplate().findByCriteria(dc);
	}
	public void updateWechat(Wechat chat){
		this.getHibernateTemplate().saveOrUpdate(chat);
	}
	
	@Override
	public List<Label> selectLabelById(String labelid) {
		// TODO Auto-generated method stub
		DetachedCriteria dc = DetachedCriteria.forClass(Label.class);
		dc.add(Restrictions.eq("id", Integer.parseInt(labelid)));
		return (List<Label>) this.getHibernateTemplate().findByCriteria(dc);
	}
	
	@Override
	public void addWechatClassMap(WechatClassMap wcm) {
		// TODO Auto-generated method stub
		this.getHibernateTemplate().saveOrUpdate(wcm);		
	}
	@Override
	public List<Integer> selectExistMaxTimes(String labelid, int wechatid) {
		Session session = this.currentSession();
		try{
			String sql = "select max(times) from WechatClassMap where classid="+ labelid + " and wechatid="+wechatid;
			return (List<Integer>)session.createQuery(sql).list();
		}catch (HibernateException e){
			e.printStackTrace();
		}
		
		return null;
	}
	

}
